<?php

namespace App\Models\Live;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ShopMemberModel extends Model
{
    use HasFactory;
    protected $connection="shop";
    protected $table="member";
    protected $primaryKey="member_id";
    public $timestamps = false;
    protected $guarded=[];
    public static function changePoints($member_id,$points){
        $member=self::getInfo($member_id);
        if(empty($member)){
            return -1;
        }
        try{
            //写个人日志
            $member_log=ShopMemberLogModel::make($member_id);
            $member_account=ShopMemberAccountModel::make([
                'member_id'=>$member_id,
                'account_data'=>$points,
                'username'=>$member->username,
                'mobile'=>$member->mobile,
                'email'=>$member->email
            ]);
            $member->increment('point',$points);
            
            $v=$member->save();
            return $v;
        }catch(Exception $e){
            Log::error('changePoints=\"'.$e->getMessage().'\"');
            return -2;
        }
    }
    public static function getInfo($member_id){
        return self::find($member_id);
    }

    /**
     * 检测是否关注
     * @param int $site_id
     * @param int $member_id
     * @return number
     */
    public static function isSubscribeMe($shop_id,$member_id){
        $isSub=DB::connection("shop")->table("shop_member")->where([
            'site_id'=>$shop_id,
            'member_id'=>$member_id,
            'is_subscribe'=>1
        ])->count('id');
        Log::debug("isSub={$isSub}");
        return empty($isSub);
    }
}